﻿using BaoVeBienCuong.Playgame.enemy;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace BaoVeBienCuong.Playgame.army
{
    class CungThu
    {
        int count = 0;//lam anh dong
        int Loai;
        Dan[] dan = new Dan[30];
        int sodan;
        int sosao;
        string huong;
        float tamxa;
        float TocDoDanh;
        float Dame;
        float scale;
        Vector2 position;
        Vector2 Lechposition;
        Vector2 size;
        Vector2 sizeImages;
        Texture2D image;
        Texture2D imagesao;
        Vector2 index;
        public CungThu()
        {
            Loai = 1;
            sodan = 1;
            tamxa = 40;
            huong = "Bottom";
            TocDoDanh = 4;
            Dame = 5;
            position = new Vector2(0, 0);
        }
        public CungThu(int loai)
        {
            Lechposition = new Vector2(0, 0);
            setLoai(loai);
        }
        public void setLoai(int loai)
        {
            sosao = 1;
            Loai = loai;
            scale = 0.5f;
            if (loai == 0) // loại cơ bản
            {
                tamxa = 384;
                TocDoDanh = 1.5f;
                Dame = 5;
                huong = "Bottom";
                sodan = 30;
                for (int i = 0; i < sodan; i++)
                {
                    dan[i] = new Dan(0, 7, position, Dame);
                    dan[i].setEnable(false);
                }
            }
            else if (loai == 1) // nâng cấp lần 1
            {
                tamxa = 384;
                TocDoDanh = 1.0f;
                Dame = 20;
                huong = "Bottom";
                sodan = 30;
                for (int i = 0; i < sodan; i++)
                {
                    dan[i] = new Dan(1, 7, position, Dame);
                    dan[i].setEnable(false);
                }
            }
            else // nâng cấp lần 2
            {
                tamxa = 384;
                TocDoDanh = 1.5f;
                Dame = 30;
                huong = "Bottom";
                sodan = 30;
                for (int i = 0; i < sodan; i++)
                {
                    dan[i] = new Dan(2, 7, position, Dame);
                    dan[i].setEnable(false);
                }
            }
        }
        public int getLoai()
        {
            return Loai;
        }
        public void setHuong(string huong)
        {
            this.huong = huong;

            if (huong.Equals("Bottom"))
            {
                Lechposition = new Vector2(-15, -25);
            }
            else if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
            {
                Lechposition = new Vector2(0, -25);
            }
            else if (huong.Equals("Left"))
            {
                Lechposition = new Vector2(-3, -25);
            }
            else
            {
                Lechposition = new Vector2(-12, -25);
            }
        }
        public void setIndex(Vector2 chiso)
        {
            index = chiso;
            size = new Vector2(GamePage.sizeImageBackGround.X / GamePage.sizeMap.Y, GamePage.sizeImageBackGround.Y / GamePage.sizeMap.X);
            position = new Vector2(chiso.Y * size.X, chiso.X * size.Y);
        }
        public float getTamxa()
        {
            return tamxa;
        }
        public void Load(ContentManager cont)
        {
            for (int f = 0; f < sodan; f++)
            {
                dan[f].Load(cont);
            }
            imagesao = cont.Load<Texture2D>("images/towers/star");
            if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
            {
                image = cont.Load<Texture2D>("images/towers/archer_" + Loai + "1");
            }
            else if (huong.Equals("Bottom"))
            {
                image = cont.Load<Texture2D>("images/towers/archer_" + Loai + "0");
            }
            else
            {
                image = cont.Load<Texture2D>("images/towers/archer_" + Loai + "2");
            }

            if (Loai == 0)
            {
                if (huong.Equals("TopLeft") || huong.Equals("TopRight")) sizeImages = new Vector2(495, 153);
                else if (huong.Equals("Bottom")) sizeImages = new Vector2(572, 154);
                else sizeImages = new Vector2(605, 145);
            }
            else if (Loai == 1)
            {
                if (huong.Equals("TopLeft") || huong.Equals("TopRight")) sizeImages = new Vector2(690, 198);
                else if (huong.Equals("Bottom")) sizeImages = new Vector2(752, 145);
                else sizeImages = new Vector2(775, 167);
            }
            else
            {
                if (huong.Equals("TopLeft") || huong.Equals("TopRight")) sizeImages = new Vector2(800, 211);
                else if (huong.Equals("Bottom")) sizeImages = new Vector2(876, 158);
                else sizeImages = new Vector2(810, 177);
            }
        }
        public void Draw(SpriteBatch sprite)
        {
            if (huong.Equals("Bottom"))
            {
                sprite.Draw(image, position + Lechposition + GamePage.DoLech, new Rectangle((int)sizeImages.X / 4 * (count / 6), 0, (int)sizeImages.X / 4, (int)sizeImages.Y),
                 Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                size = new Vector2(sizeImages.X * scale / 4, sizeImages.Y * scale);
                count++;
                if (count >= 24) count = 0;
            }
            else
            {
                if (huong.Equals("Left") || huong.Equals("TopLeft"))
                {
                    sprite.Draw(image, position + Lechposition + GamePage.DoLech, new Rectangle((int)sizeImages.X / 5 * (count / 6), 0, (int)sizeImages.X / 5, (int)sizeImages.Y),
                                            Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                }
                else
                {
                    sprite.Draw(image, position + Lechposition + GamePage.DoLech, new Rectangle((int)sizeImages.X / 5 * (count / 6), 0, (int)sizeImages.X / 5, (int)sizeImages.Y),
                                            Color.White, 0, Vector2.Zero, scale, SpriteEffects.FlipHorizontally, 0);
                }
                size = new Vector2(sizeImages.X * scale / 5, sizeImages.Y * scale);
                count++;
                if (count == 30) count = 0;
            }
            for (int k = 1; k <= sosao; k++)
            {
                sprite.Draw(imagesao, position + Lechposition + GamePage.DoLech + new Vector2(30 + 10 * k, -8), new Rectangle(0, 0, 16, 16),
                                             Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
            }
            for (int i = 0; i < sodan; i++)
            {
                if (dan[i].getEnable())
                {
                    dan[i].Draw(sprite);
                }
            }
        }
        public void Ban(Enemy[] enemy)
        {
            bool dangban = false;
            for (int j = 0; j < enemy.Length; j++)
            {
                if (tamxa >= Vector2.Distance(position, enemy[j].getPosition()) && enemy[j].getEnable())
                {
                    Quay(enemy[j]);
                    dangban = true;
                    if (count % (20 - TocDoDanh * 2) == 0)
                    {
                        for (int i = 0; i < sodan; i++)
                        {
                            if (!dan[i].getEnable())
                            {
                                dan[i].setDuongdi(position + new Vector2(20, 5), enemy[j].getTam());
                                dan[i].setEnable(true);
                                break;
                            }
                        }
                    }
                }
            }
            if (dangban == false)
            {
                huong = "Bottom";
            }
            for (int i = 0; i < sodan; i++)
            {
                if (dan[i].getEnable())
                    dan[i].Dichuyen(enemy);
            }
        }
        public void Quay(Enemy enemy)
        {
            if (enemy.getIndex().X < index.X)
            {
                if (enemy.getIndex().Y > index.Y)
                {
                    huong = "TopRight";
                }
                else
                {
                    huong = "TopLeft";
                }
            }
            else
            {
                if (enemy.getIndex().Y > index.Y)
                {
                    huong = "Right";
                }
                else
                {
                    huong = "Left";
                }
            }
        }
    }
}
